package com.sqsf.util;

import com.sqsf.bean.User;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class UserMapperTest {
    SqlSession session;
    UserMapper mapper;

    @Before
    public void before() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
        mapper = session.getMapper(UserMapper.class);

    }

    @After
    public void after() {
        //5、关闭资源
        session.close();
    }

    @Test
    //搭建Mybatis框架并完成测试
    public void test1(){
        System.out.println(mapper);
    }

    @Test
    //使用动态sql根据传入对象字段是否存在数据完成数据修改功能
    public void test2(){
        User user = new User();
        user.setUid(1);
        user.setUsername("蔡徐坤");
        boolean b = mapper.updateByUser(user);
        System.out.println(b);
    }

    @Test
    //使用动态sql完成传入数组的批量删除功能
    public void test3(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(6);
        list.add(7);
        boolean b = mapper.deleteByArray(list);
        System.out.println(b);
    }
}
